'''
Created on Nov 14, 2011
@author: marcosvariz
'''
import pygame
from SimFloresta import *
from InfoWindow import *
#from InfoPlot import *

#Variaveis de ambiente
X=0
Y=1
GRID_SIZE = (180,90)
#GRID_SIZE = (90,46)
UPDATE_TIME = 0
FRAME_RATE = 70


#Iniciar pygame, janela principal e relogio
WINDOW_SIZE = (GRID_SIZE[X] * PATCH_SIZE[X],GRID_SIZE[Y] * PATCH_SIZE[Y])
pygame.init()
SCREEN = pygame.display.set_mode(WINDOW_SIZE)
clock = pygame.time.Clock()

#Iniciar Janela de Info e InfoPlot
info = InfoWindow(SCREEN)



#Iniciar Floresta
floresta = SimFloresta(SCREEN,GRID_SIZE)
floresta.setupInit(10)
pygame.display.flip()

#Contadores e variaveis de controle
estacaoPrimavera = True
ano = 0
pause = True
updateClick = pygame.time.get_ticks()
infoActivo = False
refreshFlorestaFlag = False
densidade = 0
gGlobal = 0.01

#Editar Grelha Inicial com comandos do Rato
edit = False
waterGridX = 5
waterGridY = 5
while pause:
    
    for event in pygame.event.get():
        if event.type == pygame.MOUSEBUTTONDOWN:
            edit = True
        elif event.type == pygame.MOUSEBUTTONUP:
            edit = False
        elif event.type == pygame.KEYDOWN:
            pressed = pygame.key.get_pressed()
            if event.key == pygame.K_SPACE:
                pause = False
            elif event.key == pygame.K_q:
                exit()
            if pressed[pygame.K_r]:
                if event.key == pygame.K_UP:
                    waterGridY=min(40,waterGridY+1)
                    floresta.SetWaterGrid(waterGridX, waterGridY)
                elif event.key == pygame.K_DOWN:
                    waterGridY=max(1,waterGridY-1)
                    floresta.SetWaterGrid(waterGridX, waterGridY)
                elif event.key == pygame.K_LEFT:
                    waterGridX=min(40,waterGridX+1)
                    floresta.SetWaterGrid(waterGridX, waterGridY)
                elif event.key == pygame.K_RIGHT:
                    waterGridX=max(1,waterGridX-1)
                    floresta.SetWaterGrid(waterGridX, waterGridY)
                
            pygame.display.flip()
            
        
        if edit:
            pressed = pygame.key.get_pressed()
            cord = pygame.mouse.get_pos()
            cord = (cord[X]*GRID_SIZE[X]/WINDOW_SIZE[X],cord[Y]*GRID_SIZE[Y]/WINDOW_SIZE[Y])    
            if pressed[pygame.K_r]:
                floresta.setPatch(cord,TIPO_AGUA)
                pygame.display.flip()
            elif pressed[pygame.K_a]:
                floresta.setPatch(cord,TIPO_ARVORE)
                pygame.display.flip()
            elif pressed[pygame.K_c]:
                floresta.setPatch(cord,TIPO_CLAREIRA)
                pygame.display.flip()
                



#Ciclo principal

while True:
    #Detectar eventos
    for event in pygame.event.get():
        if event.type == pygame.KEYDOWN:
            if event.key == pygame.K_SPACE:
                pause = not pause
            elif event.key == pygame.K_i:
                if not info.flipEstado():
                    floresta.refreshScreen()
            elif event.key == pygame.K_q:
                exit()
            elif pygame.key.get_pressed()[pygame.K_r]:
                if event.key == pygame.K_UP:
                    waterGridY=min(40,waterGridY+1)
                    floresta.SetWaterGrid(waterGridX, waterGridY)
                elif event.key == pygame.K_DOWN:
                    waterGridY=max(1,waterGridY-1)
                    floresta.SetWaterGrid(waterGridX, waterGridY)
                elif event.key == pygame.K_LEFT:
                    waterGridX=min(40,waterGridX+1)
                    floresta.SetWaterGrid(waterGridX, waterGridY)
                elif event.key == pygame.K_RIGHT:
                    waterGridX=max(1,waterGridX-1)
                    floresta.SetWaterGrid(waterGridX, waterGridY)
            elif event.key == pygame.K_UP:
                gGlobal = min(gGlobal+0.01,1)
                floresta.setgGlobal(gGlobal)
            elif event.key == pygame.K_DOWN:
                gGlobal = max(gGlobal-0.01,0)
                floresta.setgGlobal(gGlobal)
            elif event.key == pygame.K_l:
                floresta.flipAprendizagem()
                
            
        elif event.type == pygame.QUIT:
            exit()
        
    
    #Update da Floresta
    if not pause:
        if pygame.time.get_ticks()-updateClick >= UPDATE_TIME:
            if estacaoPrimavera:
                #Primavera
                ano += 1
                floresta.updateStatesPrimavera()
                estacaoPrimavera = False
            else:
                #Verao
                densidade = floresta.updateStatesVerao()
                estacaoPrimavera = True
                
            updateClick = pygame.time.get_ticks()    
            
    #Desenhar janela informativa
    info.desenhar(densidade,ano,gGlobal,floresta.getAprendizagemStatus(),pause)    
    pygame.display.flip()
    
    #Controlar frame rate    
    time_passed = clock.tick(FRAME_RATE)
    
   

    
